package com.yang.refleciton;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

/**
 * @author yds
 * @program java-annotation-and-reflection
 * @description 通过反射获取泛型信息
 * @create 2020-10-12 14:58
 **/
public class Generic {
    public void test(Map<String,String> map, List<Integer> list){

    }
    public Map<String,Integer> test01(){
        return null;
    }

    public static void main(String[] args) throws NoSuchMethodException {
        Class c1 = Generic.class;
        System.out.println("-------------获取参数值的泛型参数-------------------");
        Method test = c1.getDeclaredMethod("test", Map.class, List.class);
        //获取方法的泛型参数
        Type[] genericParameterTypes = test.getGenericParameterTypes();
        for (Type genericParameterType : genericParameterTypes) {
            System.out.println(genericParameterType);
            //判断泛型参数是否属于某种包装类实例
            if(genericParameterType instanceof ParameterizedType)
            {
                Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments();
                for (Type actualTypeArgument : actualTypeArguments) {
                    System.out.println(actualTypeArgument);
                }
            }
        }

        System.out.println("-------------获取返回值的泛型参数-------------------");
        Method test01 = c1.getDeclaredMethod("test01",null);
        //获取方法的泛型参数
        Type genericReturnType = test01.getGenericReturnType();
        System.out.println(genericReturnType);
            if(genericReturnType instanceof ParameterizedType)
            {
                Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();
                for (Type actualTypeArgument : actualTypeArguments) {
                    System.out.println(actualTypeArgument);
                }
            }


    }
}